package socket.homework1;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author lhn
 * @date 2024-04-26 15:54
 * 客户端
 */

public class Homework01Client {
    public static void main(String[] args) throws IOException {
        //创建 socket 对象
        Socket socket = new Socket(InetAddress.getLocalHost(),9999);
        System.out.println("已连接服务端");
        //获取socket的输出流
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(
                new OutputStreamWriter(outputStream)
        );
        String[] strings = new String[3];
        strings[0] = "name";
        strings[1] = "hobby";
        strings[2] = "other";

        //从键盘获取用户输入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的问题:");
        String question = sc.next();

        bufferedWriter.write(question);
        bufferedWriter.newLine();
        bufferedWriter.flush();
        System.out.println("已向服务端发送消息");

        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream)
        );
        String str = bufferedReader.readLine();
        System.out.println("服务端消息内容:" + str);

        bufferedReader.close();
        bufferedWriter.close();
        socket.close();
    }
}
